<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * banner逻辑
 */
class Banner extends LogicBase
{
    
    /**
     * banner分类编辑
     */
    public function bannerCategoryEdit($data = [])
    {
        
        $validate_result = $this->validatebannerCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatebannerCategory->getError()];
        }
        
        $url = url('bannerCategoryList');
        
        $result = $this->modelbannerCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'banner分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelbannerCategory->getError()];
    }
    
    /**
     * 获取banner列表
     */
    public function getbannerList($where = [], $field = 'a.*,c.name as category_name', $order = '')
    {
        
        $this->modelbanner->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'banner_category c', 'a.cate_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelbanner->join = $join;
        $list=$this->modelbanner->getList($where, $field, $order);
        $this->modelbanner->alias('a');
        $join = [
            [SYS_DB_PREFIX . 'banner_category c', 'a.cate_id = c.id'],
        ];


        $count=$this->modelbanner->where($where)->count();
        return ['list'=>$list,'count'=>$count];
    }
    
    /**
     * 获取banner列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.title'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * banner信息编辑
     */
    public function bannerEdit($data = [])
    {
        
        $validate_result = $this->validatebanner->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatebanner->getError()];
        }
        
        $url = url('bannerList');


        
        $result = $this->modelbanner->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'banner' . $handle_text . '，name：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, 'banner操作成功', $url] : [RESULT_ERROR, $this->modelbanner->getError()];
    }

    /**
     * 获取banner信息
     */
    public function getbannerInfo($where = [], $field = 'a.*,c.name as category_name')
    {
        
        $this->modelbanner->alias('a');
        
        $join = [

                    [SYS_DB_PREFIX . 'banner_category c', 'a.cate_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelbanner->join = $join;
        
        return $this->modelbanner->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getbannerCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelbannerCategory->getInfo($where, $field);
    }
    
    /**
     * 获取banner分类列表
     */
    public function getbannerCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelbannerCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * banner分类删除
     */
    public function bannerCategoryDel($where = [])
    {
        
        $result = $this->modelbannerCategory->deleteInfo($where);
        
        $result && action_log('删除', 'banner分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'banner分类删除成功'] : [RESULT_ERROR, $this->modelbannerCategory->getError()];
    }
    
    /**
     * banner删除
     */
    public function bannerDel($where = [])
    {
        
        $result = $this->modelbanner->deleteInfo($where);
        
        $result && action_log('删除', 'banner删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'banner删除成功'] : [RESULT_ERROR, $this->modelbanner->getError()];
    }
}
